package com.midea.ai.air.ui.device.product.airconditioner.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.midea.ai.air.R;
import com.midea.ai.air.ui.device.product.utils.MetricsUtils;

public class CircleView extends View
{
	int w,h;
	double r1 = 300,r2 = 360;
	int LINESIZE = 6;
	int ROOMHUMISIZE = 10;
	int SIZE = 180;
	
	float x0,y0;
	
	float radiu; 
	float ang; 
	int humi;
	
	public CircleView(Context context)
	{
		super(context);
		init(context);
	}
	
	public CircleView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		init(context);
	}

	public CircleView(Context context, AttributeSet attrs, int defStyleAttr)
	{
		super(context, attrs, defStyleAttr);
		init(context);
	}
	
	private void init(Context context)
	{
		
	}

	@Override
	protected void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		
		r1 = MetricsUtils.Dp2Px(getContext(), 110);
		r2 = MetricsUtils.Dp2Px(getContext(), 130);
		LINESIZE = MetricsUtils.Dp2Px(getContext(), 2);
		ROOMHUMISIZE = MetricsUtils.Dp2Px(getContext(), 4);
		
		Paint paint1 = new Paint();
		paint1.setColor(Color.parseColor("#80ffffff"));
		paint1.setStrokeWidth(LINESIZE);
		Paint paint2 = new Paint();
		paint2.setColor(Color.parseColor("#FFFFFF00"));
		paint2.setStrokeWidth(ROOMHUMISIZE);
		Paint paint3 = new Paint();
		paint3.setColor(Color.parseColor("#ffffffff"));
		paint3.setStrokeWidth(LINESIZE);
		Paint paint4 = new Paint();
		paint4.setColor(Color.parseColor("#FFFFFF00"));
		paint4.setStrokeWidth(LINESIZE);
		double a = 2 * Math.PI / SIZE;
		while (a <= 2 * Math.PI)
		{
			canvas.drawLine((float)(getWidth() / 2 + r1 * Math.cos(a)), (float)(getHeight() / 2 + r1 * Math.sin(a)), (float)(getWidth() / 2 + r2 * Math.cos(a)), (float)(getHeight() / 2 + r2 * Math.sin(a)), paint1);
			a = a + 2 * Math.PI / SIZE;
		}
		
		a = humi * Math.PI / 180.0;
		
		if (radiu >= 210)
		{
			a = 2 * Math.PI + a;
			Log.d("CircleView", "a = "+a+",ang = "+ang);
			if (a < (ang + 2 * Math.PI / SIZE))
			{
				while (a <= (ang + 2 * Math.PI / SIZE))
				{
					canvas.drawLine((float)(getWidth() / 2 + r1 * Math.cos(a)), (float)(getHeight() / 2 + r1 * Math.sin(a)), (float)(getWidth() / 2 + r2 * Math.cos(a)), (float)(getHeight() / 2 + r2 * Math.sin(a)), paint4);
					a = a + 2 * Math.PI / SIZE;
				}
			}
			else
			{
				while (a >= (ang + 2 * Math.PI / SIZE))
				{
					canvas.drawLine((float)(getWidth() / 2 + r1 * Math.cos(a)), (float)(getHeight() / 2 + r1 * Math.sin(a)), (float)(getWidth() / 2 + r2 * Math.cos(a)), (float)(getHeight() / 2 + r2 * Math.sin(a)), paint3);
					a = a - 2 * Math.PI / SIZE;
				}
			}
		}
		else
		{
			if (a < ang)
			{
				while ( a <= (ang + 2 * Math.PI / SIZE) && (Math.PI / 3  + 2 * Math.PI / SIZE) >= ang)
				{
					canvas.drawLine((float)(getWidth() / 2 + r1 * Math.cos(a)), (float)(getHeight() / 2 + r1 * Math.sin(a)), (float)(getWidth() / 2 + r2 * Math.cos(a)), (float)(getHeight() / 2 + r2 * Math.sin(a)), paint4);
					a = a + 2 * Math.PI / SIZE;
				}
			}
			else
			{
				while (a >= (ang + 2 * Math.PI / SIZE) )
				{
					canvas.drawLine((float)(getWidth() / 2 + r1 * Math.cos(a)), (float)(getHeight() / 2 + r1 * Math.sin(a)), (float)(getWidth() / 2 + r2 * Math.cos(a)), (float)(getHeight() / 2 + r2 * Math.sin(a)), paint3);
					a = a - 2 * Math.PI / SIZE;
				}
			}
		}
		
		canvas.drawLine((float)(getWidth() / 2 + r1 * Math.cos(humi * Math.PI / 180.0)), (float)(getHeight() / 2 + r1 * Math.sin(humi * Math.PI / 180.0)), (float)(getWidth() / 2 + r2 * Math.cos(humi * Math.PI / 180.0)), (float)(getHeight() / 2 + r2 * Math.sin(humi * Math.PI / 180.0)), paint2);
		
		Paint bitmappPaint = new Paint();
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.ac_round_mark);

		Bitmap bitmap2 = small(bitmap);
		canvas.rotate(radiu - 2, (float) (getWidth() / 2), (float) (getHeight() / 2));
		//bitmappPaint.setAlpha(200);
		canvas.drawBitmap(bitmap2, (float) (getWidth() / 2), (float) (getHeight() / 2 - (r2 + 30)), bitmappPaint);
	}
	
	private Bitmap small(Bitmap bitmap) {
		Matrix matrix = new Matrix();
		float y = bitmap.getHeight();
		float x = bitmap.getWidth();
		
		matrix.postScale((16.4f/14.6f)*MetricsUtils.Dp2Px(getContext(), 40)/ y, (16.4f/14.6f)*MetricsUtils.Dp2Px(getContext(), 40)/ y); // 长和宽放大缩小的比例
		Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
				bitmap.getHeight(), matrix, true);
		return resizeBmp;
	}
	
	public void doAction(float x, float y,int west,int setHumi)
	{
		humi = (west - 60) * 6 - 90;
		if (setHumi == 0)
		{
			radiu = (float) (((float) Math.atan2( y - (getHeight() / 2), x - (getWidth() / 2)) * (float)180 / Math.PI) + 90);
		}
		else
		{
			if (setHumi >= 45)
			{
				radiu = 150 + 6 * (setHumi - 85);
			}
			else
			{
				radiu = 210 + 6 * (setHumi - 35);
			}
		}
		ang = (float) ((radiu - 90) / 180.0 * Math.PI);
		invalidate();
	}
}
